﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TimeTable.AddForms;
using TimeTable.Domain.DataAccess;
using TimeTable.Domain.Entity;
using TimeTable.ReportForms;

namespace TimeTable
{
    public partial class ClientForm : Form
    {
        public ClientForm()
        {
            InitializeComponent();

            LoadDivisions();
            LoadStudyUnits();
            LoadTeachers();
            LoadRooms();
        }

        #region Divisions

        private void LoadDivisions()
        {
            divisionTypeComboBox.DataSource = new List<DivisionType>
            {
                DivisionType.University,
                DivisionType.Faculty,
                DivisionType.Chair
            };
        }

        private void DivisionTypeComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            divisionsListBox.DataSource = DataAccessConnection.Instance.GetDivisionsByType((DivisionType)divisionTypeComboBox.SelectedItem);
        }

        private void RemoveDivisionToolStripMenuItemClick(object sender, EventArgs e)
        {
            var division = divisionsListBox.SelectedItem as Division;
            if (division == null) return;
                
            DataAccessConnection.Instance.RemoveRelatedToDivision(division);
            DataAccessConnection.Instance.Remove(division);

            divisionsListBox.DataSource = DataAccessConnection.Instance.GetDivisionsByType((DivisionType)divisionTypeComboBox.SelectedItem);
        }

        #endregion

        #region StudyUnits

        private void LoadStudyUnits()
        {
            studyUnitsComboBox.DataSource = new List<StudyUnitType>
            {
                StudyUnitType.Stream,
                StudyUnitType.Group
            };
        }

        private void StudyUnitsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            studyUnitsListBox.DataSource = DataAccessConnection.Instance.GetStudyUnitsByType((StudyUnitType)studyUnitsComboBox.SelectedItem);
        }

        private void RemoveStudyUnitToolStripMenuItemClick(object sender, EventArgs e)
        {
            var studyUnit = studyUnitsListBox.SelectedItem as StudyUnit;
            if (studyUnit == null) return;

            DataAccessConnection.Instance.RemoveRelatedToStudyUnit(studyUnit);
            DataAccessConnection.Instance.Remove(studyUnit);

            studyUnitsListBox.DataSource = DataAccessConnection.Instance.GetStudyUnitsByType((StudyUnitType)studyUnitsComboBox.SelectedItem);
        }

        private void ShowTimeTableToolStripMenuItemClick(object sender, EventArgs e)
        {
            var studyUnit = studyUnitsListBox.SelectedItem as StudyUnit;
            if (studyUnit == null) return;

            var timeTableForm = new TimeTableForm(studyUnit);
            timeTableForm.ShowDialog(this);
        }

        #endregion

        #region Teachers

        private void LoadTeachers()
        {
            teachersComboBox.DataSource = DataAccessConnection.Instance.GetDivisionsByType(DivisionType.Chair);
        }

        private void TeachersComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            var division = teachersComboBox.SelectedItem as Division;

            if (division == null)
            {
                teachersListBox.DataSource = null;
                return;
            }

            teachersListBox.DataSource = DataAccessConnection.Instance.GetTeachersByDivision(division);
        }

        private void RemoveTeacherToolStripMenuItemClick(object sender, EventArgs e)
        {
            var teacher = teachersListBox.SelectedItem as Teacher;
            if (teacher == null) return;

            DataAccessConnection.Instance.RemoveRelatedToTeacher(teacher);
            DataAccessConnection.Instance.Remove(teacher);

            var division = teachersComboBox.SelectedItem as Division;

            if (division == null)
            {
                teachersListBox.DataSource = null;
                return;
            }

            teachersListBox.DataSource = DataAccessConnection.Instance.GetTeachersByDivision(division);
        }

        #endregion

        #region Rooms

        private void LoadRooms()
        {
            roomsListBox.DataSource = DataAccessConnection.Instance.GetAllRooms();
        }

        private void RemoveRoomToolStripMenuItemClick(object sender, EventArgs e)
        {
            var room = roomsListBox.SelectedItem as Room;
            if (room == null) return;

            DataAccessConnection.Instance.RemoveRelatedToRoom(room);
            DataAccessConnection.Instance.Remove(room);

            roomsListBox.DataSource = DataAccessConnection.Instance.GetAllRooms();
        }

        #endregion

        #region Menu

        private void RefreshToolStripMenuItemClick(object sender, EventArgs e)
        {
            LoadDivisions();
            LoadStudyUnits();
            LoadTeachers();
            LoadRooms();
        }

        private void RoomToolStripMenuItemClick(object sender, EventArgs e)
        {
            var newRoomForm = new NewRoomForm();
            newRoomForm.ShowDialog(this);
        }

        private void UniToolStripMenuItemClick(object sender, EventArgs e)
        {
            var newDivisionForm = new NewDivisionForm(DivisionType.University);
            newDivisionForm.ShowDialog(this);
        }

        private void FacultyToolStripMenuItemClick(object sender, EventArgs e)
        {
            var newDivisionForm = new NewDivisionForm(DivisionType.Faculty);
            newDivisionForm.ShowDialog(this);
        }

        private void ChairToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var newDivisionForm = new NewDivisionForm(DivisionType.Chair);
            newDivisionForm.ShowDialog(this);
        }

        private void TeacherToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var newTeacherForm = new NewTeacherForm();
            newTeacherForm.ShowDialog(this);
        }

        private void StreamToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var studyUnitForm = new NewStudyUnitForm(StudyUnitType.Stream);
            studyUnitForm.ShowDialog(this);
        }

        private void GroupToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var studyUnitForm = new NewStudyUnitForm(StudyUnitType.Group);
            studyUnitForm.ShowDialog(this);
        }

        private void TimeTableToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var newTimeTableForm = new NewTimeTableForm();
            newTimeTableForm.ShowDialog(this);
        }

        private void AboutToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var aboutForm = new AboutForm();
            aboutForm.ShowDialog(this);
        }

        private void LessonToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            var newTimeTableUnitForm = new NewTimeTableUnitForm();
            newTimeTableUnitForm.ShowDialog(this);
        }

        #endregion
    }
}
